﻿using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.WebControls;
using System.Reflection;
using System.ComponentModel;
using System.Security;
using System.Security.Permissions;
using System.Xml;

namespace PickGold.Web.Controls
{
	/// <summary>
	/// 一个数据绑定列表控件，允许通过为列表中显示的每一项重复指定的模板来自定义布局。
	/// </summary>
	[Bindable(true)]
	[ParseChildren(true)]
	[PersistChildren(false)]
	[ToolboxData("<{0}:WebRepeater runat=\"server\" />")]
	public class WebRepeater : Repeater
	{
		private NameValueCollection _Arguments;
		private ITemplate _ArgumentTemplate;
		private ITemplate _EmptyTemplate;
		private int _StartIndex;
		private int _MaxRows;

		/// <summary>
		/// 参数
		/// </summary>
		[Browsable(false)]
		public NameValueCollection Arguments
		{
			get
			{
				if (this._Arguments == null)
					this._Arguments = new NameValueCollection();
				return this._Arguments;
			}
		}

		/// <summary>
		/// 开始索引
		/// </summary>
		[Bindable(true)]
		[DisplayName("开始索引")]
		[Description("开始索引")]
		public int StartIndex
		{
			set
			{
				this._StartIndex = value;
			}
			get
			{
				return this._StartIndex;
			}
		}

		/// <summary>
		/// 最大行数
		/// </summary>
		[Bindable(true)]
		[DisplayName("最大行数")]
		[Description("最大行数")]
		public int MaxRows
		{
			set
			{
				this._MaxRows = value;
			}
			get
			{
				return this._MaxRows;
			}
		}

		/// <summary>
		/// 参数
		/// </summary>
		[Bindable(true)]
		[Browsable(false)]
		[DisplayName("参数")]
		[Description("参数")]
		public string Argument
		{
			set
			{
				if (string.IsNullOrEmpty(value))
					return;

				if (value[0] == AsciiChar.LT && value[value.Length - 1] == AsciiChar.GT)
				{
					var nc = 0;
					var ns = new NameValueCollection();
					var xml = new XmlDocument();
					xml.LoadXml(value);
					foreach (XmlAttribute a in xml.DocumentElement.Attributes)
						ns[a.Name] = a.Value;
					foreach (XmlNode n in xml.DocumentElement.ChildNodes)
					{
						if (n.NodeType == XmlNodeType.Element)
						{
							ns[n.Name] = n.InnerText;
							nc++;
						}
					}
					if (nc == 0)
						ns[string.Empty] = xml.DocumentElement.InnerText;
					this._Arguments = ns;
					return;
				}

				var qs = HttpUtility.ParseQueryString(value);
				//if (this._Arguments == null)
				//    this._Arguments = qs;
				//else
				//    this._Arguments.Add(qs);
				this._Arguments = qs;
			}
			get
			{
				if (this._Arguments == null || this._Arguments.Count == 0)
					return null;

				//var t = string.Empty;
				//for (var i = 0; i < this._Arguments.Count; i++)
				//{
				//    var k = this._Arguments.GetKey(i);
				//    var vs = this._Arguments.GetValues(i);
				//    var qk = "&" + HttpUtility.UrlEncode(k) + "=";
				//    foreach (var v in vs)
				//        t = t + qk + HttpUtility.UrlEncode(v);
				//}
				//if (t.Length > 0)
				//    return t.Substring(1);

				//return t;
				return this._Arguments.ToString();
			}
		}

		/// <summary>
		/// 空模板
		/// </summary>
		[DefaultValue((string) null)]
		[TemplateContainer(typeof(RepeaterItem))]
		[Description("空模板！")]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[Browsable(true)]
		public ITemplate EmptyTemplate
		{
			set
			{
				this._EmptyTemplate = value;
				if (value == null)
					return;

				var control = this.CreateItem(-1, ListItemType.Item);
				value.InstantiateIn(control);
				this.Controls.Add(control);
			}
			get
			{
				return this._EmptyTemplate;
			}
		}

		/// <summary>
		/// 参数模板
		/// </summary>
		[DefaultValue((string) null)]
		[TemplateContainer(typeof(RepeaterItem))]
		[Description("参数模板！")]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[Browsable(true)]
		public ITemplate ArgumentTemplate
		{
			set
			{
				this._ArgumentTemplate = value;
				if (value == null)
					return;

				var control = this.CreateItem(-1, (ListItemType)int.MinValue);
				value.InstantiateIn(control);
				//this.Controls.Add(control);
				var sw = new StringWriter();
				var htw = new HtmlTextWriter(sw);
				control.RenderControl(htw);
				this.Argument = sw.ToString().Trim();
				htw.Dispose();
				sw.Dispose();
			}
			get
			{
				return _ArgumentTemplate;
			}
		}

		/// <summary>
		/// 返回 System.Web.UI.DataSourceSelectArguments.Empty 值。
		/// </summary>
		/// <returns>System.Web.UI.DataSourceSelectArguments.Empty 值。</returns>
		protected override DataSourceSelectArguments CreateDataSourceSelectArguments()
		{
			return new DataSourceSelectArguments(this._StartIndex, this._MaxRows);
		}

		/// <summary>
		/// 创建一个带或不带指定数据源的控件层次结构。
		/// </summary>
		/// <param name="useDataSource">指示是否要使用指定的数据源。</param>
		protected override void CreateControlHierarchy(bool useDataSource)
		{
			base.CreateControlHierarchy(useDataSource);
			if (this._EmptyTemplate == null || base.Items.Count > 0)
				return;

			this.Controls.Clear();
			//if (this.HeaderTemplate != null)
			//{
			//    var header = this.CreateItem(-1, ListItemType.Header);
			//    this.HeaderTemplate.InstantiateIn(header);
			//    this.Controls.Add(header);
			//}
			var empty = this.CreateItem(-1, ListItemType.Item);
			this._EmptyTemplate.InstantiateIn(empty);
			this.Controls.Add(empty);
			//if (this.FooterTemplate != null)
			//{
			//    var footer = this.CreateItem(-1, ListItemType.Footer);
			//    this.HeaderTemplate.InstantiateIn(footer);
			//    this.Controls.Add(footer);
			//}
		}
		
		private void RenderMethod(HtmlTextWriter output, Control container)
		{
			container.RenderControl(output);
		}
	}









	//[DefaultEvent("ItemCommand"), DefaultProperty("DataSource"), ParseChildren(true), PersistChildren(false), Designer("System.Web.UI.Design.WebControls.RepeaterDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), AspNetHostingPermission(SecurityAction.LinkDemand, Level=AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level=AspNetHostingPermissionLevel.Minimal)]
	class Repeater0 : Control, INamingContainer
	{
		#region inner class

		class DummyDataSource : ICollection, IEnumerable
		{
			// Fields
			private int dataItemCount;

			// Methods
			internal DummyDataSource(int dataItemCount)
			{
				this.dataItemCount = dataItemCount;
			}

			public void CopyTo(Array array, int index)
			{
				IEnumerator enumerator = this.GetEnumerator();
				while (enumerator.MoveNext())
				{
					array.SetValue(enumerator.Current, index++);
				}
			}

			public IEnumerator GetEnumerator()
			{
				return new DummyDataSourceEnumerator(this.dataItemCount);
			}

			// Properties
			public int Count
			{
				get
				{
					return this.dataItemCount;
				}
			}

			public bool IsSynchronized
			{
				get
				{
					return false;
				}
			}

			public object SyncRoot
			{
				get
				{
					return this;
				}
			}

			// Nested Types
			private class DummyDataSourceEnumerator : IEnumerator
			{
				// Fields
				private int count;
				private int index;

				// Methods
				public DummyDataSourceEnumerator(int count)
				{
					this.count = count;
					this.index = -1;
				}

				public bool MoveNext()
				{
					this.index++;
					return (this.index < this.count);
				}

				public void Reset()
				{
					this.index = -1;
				}

				// Properties
				public object Current
				{
					get
					{
						return null;
					}
				}
			}
		}

		class ReadOnlyDataSource : IDataSource
		{
			// Fields
			private string _dataMember;
			private object _dataSource;
			private static string[] ViewNames = new string[0];

			// Events
			event EventHandler IDataSource.DataSourceChanged
			{
				add
				{
				}
				remove
				{
				}
			}

			// Methods
			public ReadOnlyDataSource(object dataSource, string dataMember)
			{
				this._dataSource = dataSource;
				this._dataMember = dataMember;
			}

			DataSourceView IDataSource.GetView(string viewName)
			{
				IDataSource source = this._dataSource as IDataSource;
				if (source != null)
				{
					return source.GetView(viewName);
				}
				return new ReadOnlyDataSourceView(this, this._dataMember, DataSourceHelper.GetResolvedDataSource(this._dataSource, this._dataMember));
			}

			ICollection IDataSource.GetViewNames()
			{
				return ViewNames;
			}
		}

		class ReadOnlyDataSourceView : DataSourceView
		{
			// Fields
			private IEnumerable _dataSource;

			// Methods
			public ReadOnlyDataSourceView(ReadOnlyDataSource owner, string name, IEnumerable dataSource)
				: base(owner, name)
			{
				this._dataSource = dataSource;
			}

			protected override IEnumerable ExecuteSelect(DataSourceSelectArguments arguments)
			{
				arguments.RaiseUnsupportedCapabilitiesError(this);
				return this._dataSource;
			}
		}

		class DataSourceHelper
		{
			// Methods
			private DataSourceHelper()
			{
			}

			internal static IEnumerable GetResolvedDataSource(object dataSource, string dataMember)
			{
				if (dataSource != null)
				{
					IListSource source = dataSource as IListSource;
					if (source != null)
					{
						IList list = source.GetList();
						if (!source.ContainsListCollection)
						{
							return list;
						}
						if ((list != null) && (list is ITypedList))
						{
							PropertyDescriptorCollection itemProperties = ((ITypedList)list).GetItemProperties(new PropertyDescriptor[0]);
							if ((itemProperties == null) || (itemProperties.Count == 0))
							{
								throw new HttpException("ListSource_Without_DataMembers");
							}
							PropertyDescriptor descriptor = null;
							if (string.IsNullOrEmpty(dataMember))
							{
								descriptor = itemProperties[0];
							}
							else
							{
								descriptor = itemProperties.Find(dataMember, true);
							}
							if (descriptor != null)
							{
								object component = list[0];
								object obj3 = descriptor.GetValue(component);
								if ((obj3 != null) && (obj3 is IEnumerable))
								{
									return (IEnumerable)obj3;
								}
							}
							throw new HttpException("ListSource_Missing_DataMember");
						}
					}
					if (dataSource is IEnumerable)
					{
						return (IEnumerable)dataSource;
					}
				}
				return null;
			}
		}

		class DataBoundControlHelper
		{
			// Methods
			public static bool CompareStringArrays(string[] stringA, string[] stringB)
			{
				if ((stringA != null) || (stringB != null))
				{
					if ((stringA == null) || (stringB == null))
					{
						return false;
					}
					if (stringA.Length != stringB.Length)
					{
						return false;
					}
					for (int i = 0; i < stringA.Length; i++)
					{
						if (!string.Equals(stringA[i], stringB[i], StringComparison.Ordinal))
						{
							return false;
						}
					}
				}
				return true;
			}

			internal static void ExtractValuesFromBindableControls(IOrderedDictionary dictionary, Control container)
			{
				IBindableControl control = container as IBindableControl;
				if (control != null)
				{
					control.ExtractValues(dictionary);
				}
				foreach (Control control2 in container.Controls)
				{
					ExtractValuesFromBindableControls(dictionary, control2);
				}
			}

			public static Control FindControl(Control control, string controlID)
			{
				Control namingContainer = control;
				Control control3 = null;
				if (control != control.Page)
				{
					while ((control3 == null) && (namingContainer != control.Page))
					{
						namingContainer = namingContainer.NamingContainer;
						if (namingContainer == null)
						{
							throw new HttpException("DataBoundControlHelper_NoNamingContainer");
						}
						control3 = namingContainer.FindControl(controlID);
					}
					return control3;
				}
				return control.FindControl(controlID);
			}

			public static bool IsBindableType(Type type)
			{
				if (type == null)
				{
					return false;
				}
				Type underlyingType = Nullable.GetUnderlyingType(type);
				if (underlyingType != null)
				{
					type = underlyingType;
				}
				if (((!type.IsPrimitive && (type != typeof(string))) && ((type != typeof(DateTime)) && (type != typeof(decimal)))) && ((type != typeof(Guid)) && (type != typeof(DateTimeOffset))))
				{
					return (type == typeof(TimeSpan));
				}
				return true;
			}
		}

		class DataSource0 : IDataSource
		{
			private DataSourceView _View;

			public DataSource0(DataSourceView view)
			{
				this._View = view;
			}

			public DataSourceView GetView(string viewName)
			{
				if (this._View != null)
					return this._View;

				return new DataSourceView0(this, viewName);
			}

			public ICollection GetViewNames()
			{
				if (this._View != null)
					return new List<string> { this._View.Name };

				return null;
			}

			public event EventHandler DataSourceChanged;
		}

		class DataSourceView0 : DataSourceView
		{
			private DataSourceView _View;

			public DataSourceView0(IDataSource owner, string viewName) : base(owner, viewName) { }

			public DataSourceView0(DataSourceView view) : this(new DataSource0(view), view.Name)
			{
				this._View = view;
			}

			protected override IEnumerable ExecuteSelect(DataSourceSelectArguments arguments)
			{
				var m = MethodBase.GetCurrentMethod();
				var t = typeof(Func<DataSourceSelectArguments, IEnumerable>);
				var h = Delegate.CreateDelegate(t, this._View, m.Name);
				return h.DynamicInvoke(arguments) as IEnumerable;
			}

			public static IEnumerable ExecuteSelect(DataSourceView view, DataSourceSelectArguments arguments)
			{
				//var m = MethodBase.GetCurrentMethod();
				//var t = typeof(Func<DataSourceSelectArguments, IEnumerable>);
				//var h = Delegate.CreateDelegate(t, view, m.Name);
				//return h.DynamicInvoke(arguments) as IEnumerable;
				var o = new DataSourceView0(view);
				return o.ExecuteSelect(arguments);
			}
		}

		#endregion

		// Fields
		private DataSourceSelectArguments _arguments;
		private DataSourceView _currentView;
		private bool _currentViewIsFromDataSourceID;
		private bool _currentViewValid;
		private bool _inited;
		private bool _pagePreLoadFired;
		private bool _requiresDataBinding;
		private bool _throwOnDataPropertyChange;
		private ITemplate alternatingItemTemplate;
		private object dataSource;
		private static readonly object EventItemCommand = new object();
		private static readonly object EventItemCreated = new object();
		private static readonly object EventItemDataBound = new object();
		private ITemplate footerTemplate;
		private ITemplate headerTemplate;
		internal const string ItemCountViewStateKey = "_!ItemCount";
		private ArrayList itemsArray;
		private RepeaterItemCollection itemsCollection;
		private ITemplate itemTemplate;
		private ITemplate separatorTemplate;

		// Events
		[Category("Action"), Description("Repeater_OnItemCommand")]
		public event RepeaterCommandEventHandler ItemCommand
		{
			add
			{
				base.Events.AddHandler(EventItemCommand, value);
			}
			remove
			{
				base.Events.RemoveHandler(EventItemCommand, value);
			}
		}

		[Category("Behavior"), Description("DataControls_OnItemCreated")]
		public event RepeaterItemEventHandler ItemCreated
		{
			add
			{
				base.Events.AddHandler(EventItemCreated, value);
			}
			remove
			{
				base.Events.RemoveHandler(EventItemCreated, value);
			}
		}

		[Category("Behavior"), Description("DataControls_OnItemDataBound")]
		public event RepeaterItemEventHandler ItemDataBound
		{
			add
			{
				base.Events.AddHandler(EventItemDataBound, value);
			}
			remove
			{
				base.Events.RemoveHandler(EventItemDataBound, value);
			}
		}

		// Methods
		private DataSourceView ConnectToDataSourceView()
		{
			if (!this._currentViewValid || base.DesignMode)
			{
				if ((this._currentView != null) && this._currentViewIsFromDataSourceID)
				{
					this._currentView.DataSourceViewChanged -= new EventHandler(this.OnDataSourceViewChanged);
				}
				IDataSource source = null;
				string dataSourceID = this.DataSourceID;
				if (dataSourceID.Length != 0)
				{
					Control control = DataBoundControlHelper.FindControl(this, dataSourceID);
					if (control == null)
					{
						throw new HttpException("DataControl_DataSourceDoesntExist");
					}
					source = control as IDataSource;
					if (source == null)
					{
						throw new HttpException("DataControl_DataSourceIDMustBeDataControl");
					}
				}
				if (source == null)
				{
					source = new ReadOnlyDataSource(this.DataSource, this.DataMember);
				}
				else if (this.DataSource != null)
				{
					throw new InvalidOperationException("DataControl_MultipleDataSources");
				}
				DataSourceView view = source.GetView(this.DataMember);
				if (view == null)
				{
					throw new InvalidOperationException("DataControl_ViewNotFound");
				}
				this._currentViewIsFromDataSourceID = this.IsBoundUsingDataSourceID;
				this._currentView = view;
				if ((this._currentView != null) && this._currentViewIsFromDataSourceID)
				{
					this._currentView.DataSourceViewChanged += new EventHandler(this.OnDataSourceViewChanged);
				}
				this._currentViewValid = true;
			}
			return this._currentView;
		}

		protected override void CreateChildControls()
		{
			this.Controls.Clear();
			if (this.ViewState["_!ItemCount"] != null)
			{
				this.CreateControlHierarchy(false);
			}
			else
			{
				this.itemsArray = new ArrayList();
			}
			base.ClearChildViewState();
		}

		protected virtual void CreateControlHierarchy(bool useDataSource)
		{
			IEnumerable data = null;
			int dataItemCount = -1;
			if (this.itemsArray != null)
				this.itemsArray.Clear();
			else
				this.itemsArray = new ArrayList();
			if (!useDataSource)
			{
				dataItemCount = (int) this.ViewState["_!ItemCount"];
				if (dataItemCount != -1)
				{
					data = new DummyDataSource(dataItemCount);
					this.itemsArray.Capacity = dataItemCount;
				}
			}
			else
			{
				data = this.GetData();
				var is2 = data as ICollection;
				if (is2 != null)
				{
					this.itemsArray.Capacity = is2.Count;
				}
			}
			if (data != null)
			{
				var itemIndex = 0;
				var flag = this.separatorTemplate != null;
				dataItemCount = 0;
				if (this.headerTemplate != null)
				{
					this.CreateItem(-1, ListItemType.Header, useDataSource, null);
				}
				foreach (var obj2 in data)
				{
					if (flag && (dataItemCount > 0))
					{
						this.CreateItem(itemIndex - 1, ListItemType.Separator, useDataSource, null);
					}
					var itemType = ((itemIndex % 2) == 0) ? ListItemType.Item : ListItemType.AlternatingItem;
					var item = this.CreateItem(itemIndex, itemType, useDataSource, obj2);
					this.itemsArray.Add(item);
					dataItemCount++;
					itemIndex++;
				}
				if (this.footerTemplate != null)
				{
					this.CreateItem(-1, ListItemType.Footer, useDataSource, null);
				}
			}
			if (useDataSource)
			{
				this.ViewState["_!ItemCount"] = (data != null) ? dataItemCount : -1;
			}
		}

		protected virtual DataSourceSelectArguments CreateDataSourceSelectArguments()
		{
			return DataSourceSelectArguments.Empty;
		}

		protected virtual RepeaterItem CreateItem(int itemIndex, ListItemType itemType)
		{
			return new RepeaterItem(itemIndex, itemType);
		}

		private RepeaterItem CreateItem(int itemIndex, ListItemType itemType, bool dataBind, object dataItem)
		{
			var item = this.CreateItem(itemIndex, itemType);
			var e = new RepeaterItemEventArgs(item);
			this.InitializeItem(item);
			if (dataBind)
			{
				item.DataItem = dataItem;
			}
			this.OnItemCreated(e);
			this.Controls.Add(item);
			if (dataBind)
			{
				item.DataBind();
				this.OnItemDataBound(e);
				item.DataItem = null;
			}
			return item;
		}

		public override void DataBind()
		{
			if ((!this.IsBoundUsingDataSourceID || !base.DesignMode) || (base.Site != null))
			{
				this.RequiresDataBinding = false;
				this.OnDataBinding(EventArgs.Empty);
			}
		}

		protected void EnsureDataBound()
		{
			try
			{
				this._throwOnDataPropertyChange = true;
				if (this.RequiresDataBinding && (this.DataSourceID.Length > 0))
				{
					this.DataBind();
				}
			}
			finally
			{
				this._throwOnDataPropertyChange = false;
			}
		}

		protected virtual IEnumerable GetData()
		{
			var view = this.ConnectToDataSourceView();
			if (view != null)
			{
				return DataSourceView0.ExecuteSelect(view, this.SelectArguments);// view.ExecuteSelect(this.SelectArguments, null);//
			}
			return null;
		}

		protected virtual void InitializeItem(RepeaterItem item)
		{
			ITemplate itemTemplate = null;
			switch (item.ItemType)
			{
				case ListItemType.Header:
					itemTemplate = this.headerTemplate;
					goto Label_005B;

				case ListItemType.Footer:
					itemTemplate = this.footerTemplate;
					goto Label_005B;

				case ListItemType.Item:
					break;

				case ListItemType.AlternatingItem:
					itemTemplate = this.alternatingItemTemplate;
					if (itemTemplate != null)
					{
						goto Label_005B;
					}
					break;

				case ListItemType.Separator:
					itemTemplate = this.separatorTemplate;
					goto Label_005B;

				default:
					goto Label_005B;
			}
			itemTemplate = this.itemTemplate;
		Label_005B:
			if (itemTemplate != null)
			{
				itemTemplate.InstantiateIn(item);
			}
		}

		protected override bool OnBubbleEvent(object sender, EventArgs e)
		{
			bool flag = false;
			if (e is RepeaterCommandEventArgs)
			{
				this.OnItemCommand((RepeaterCommandEventArgs) e);
				flag = true;
			}
			return flag;
		}

		protected override void OnDataBinding(EventArgs e)
		{
			base.OnDataBinding(e);
			this.Controls.Clear();
			base.ClearChildViewState();
			this.CreateControlHierarchy(true);
			base.ChildControlsCreated = true;
		}

		protected virtual void OnDataPropertyChanged()
		{
			if (this._throwOnDataPropertyChange)
			{
				throw new HttpException("DataBoundControl_InvalidDataPropertyChange");
			}
			if (this._inited)
			{
				this.RequiresDataBinding = true;
			}
			this._currentViewValid = false;
		}

		protected virtual void OnDataSourceViewChanged(object sender, EventArgs e)
		{
			this.RequiresDataBinding = true;
		}

		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);
			if (this.Page != null)
			{
				this.Page.PreLoad += new EventHandler(this.OnPagePreLoad);
				if (!base.IsViewStateEnabled && this.Page.IsPostBack)
				{
					this.RequiresDataBinding = true;
				}
			}
		}

		protected virtual void OnItemCommand(RepeaterCommandEventArgs e)
		{
			RepeaterCommandEventHandler handler = (RepeaterCommandEventHandler) base.Events[EventItemCommand];
			if (handler != null)
			{
				handler(this, e);
			}
		}

		protected virtual void OnItemCreated(RepeaterItemEventArgs e)
		{
			RepeaterItemEventHandler handler = (RepeaterItemEventHandler) base.Events[EventItemCreated];
			if (handler != null)
			{
				handler(this, e);
			}
		}

		protected virtual void OnItemDataBound(RepeaterItemEventArgs e)
		{
			RepeaterItemEventHandler handler = (RepeaterItemEventHandler) base.Events[EventItemDataBound];
			if (handler != null)
			{
				handler(this, e);
			}
		}

		protected override void OnLoad(EventArgs e)
		{
			this._inited = true;
			this.ConnectToDataSourceView();
			if (((this.Page != null) && !this._pagePreLoadFired) && (this.ViewState["_!ItemCount"] == null))
			{
				if (!this.Page.IsPostBack)
				{
					this.RequiresDataBinding = true;
				}
				else if (base.IsViewStateEnabled)
				{
					this.RequiresDataBinding = true;
				}
			}
			base.OnLoad(e);
		}

		private void OnPagePreLoad(object sender, EventArgs e)
		{
			this._inited = true;
			if (this.Page != null)
			{
				this.Page.PreLoad -= new EventHandler(this.OnPagePreLoad);
				if (!this.Page.IsPostBack)
				{
					this.RequiresDataBinding = true;
				}
				if ((this.Page.IsPostBack && base.IsViewStateEnabled) && (this.ViewState["_!ItemCount"] == null))
				{
					this.RequiresDataBinding = true;
				}
				this._pagePreLoadFired = true;
			}
		}

		protected override void OnPreRender(EventArgs e)
		{
			this.EnsureDataBound();
			base.OnPreRender(e);
		}

		// Properties
		[DefaultValue((string) null)]
		[TemplateContainer(typeof(RepeaterItem))]
		[Description("Repeater_AlternatingItemTemplate")]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[Browsable(false)]
		public virtual ITemplate AlternatingItemTemplate
		{
			get
			{
				return this.alternatingItemTemplate;
			}
			set
			{
				this.alternatingItemTemplate = value;
			}
		}

		public override ControlCollection Controls
		{
			get
			{
				this.EnsureChildControls();
				return base.Controls;
			}
		}

		[Description("Repeater_DataMember")]
		[DefaultValue("")]
		[Category("Data")]
		public virtual string DataMember
		{
			get
			{
				object obj2 = this.ViewState["DataMember"];
				if (obj2 != null)
				{
					return (string) obj2;
				}
				return string.Empty;
			}
			set
			{
				this.ViewState["DataMember"] = value;
				this.OnDataPropertyChanged();
			}
		}

		[Bindable(true)]
		[Description("BaseDataBoundControl_DataSource")]
		[Category("Data")]
		[DefaultValue((string) null)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public virtual object DataSource
		{
			get
			{
				return this.dataSource;
			}
			set
			{
				if (((value != null) && !(value is IListSource)) && !(value is IEnumerable))
				{
					throw new ArgumentException("Invalid_DataSource_Type");
				}
				this.dataSource = value;
				this.OnDataPropertyChanged();
			}
		}

		[Description("BaseDataBoundControl_DataSourceID")]
		[DefaultValue("")]
		[IDReferenceProperty(typeof(DataSourceControl))]
		[Category("Data")]
		public virtual string DataSourceID
		{
			get
			{
				object obj2 = this.ViewState["DataSourceID"];
				if (obj2 != null)
				{
					return (string) obj2;
				}
				return string.Empty;
			}
			set
			{
				this.ViewState["DataSourceID"] = value;
				this.OnDataPropertyChanged();
			}
		}

		[Browsable(true)]
		public override bool EnableTheming
		{
			get
			{
				return base.EnableTheming;
			}
			set
			{
				base.EnableTheming = value;
			}
		}

		[Browsable(false)]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[TemplateContainer(typeof(RepeaterItem))]
		[Description("Repeater_FooterTemplate")]
		[DefaultValue((string) null)]
		public virtual ITemplate FooterTemplate
		{
			get
			{
				return this.footerTemplate;
			}
			set
			{
				this.footerTemplate = value;
			}
		}

		[DefaultValue((string) null)]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[TemplateContainer(typeof(RepeaterItem))]
		[Description("WebControl_HeaderTemplate")]
		[Browsable(false)]
		public virtual ITemplate HeaderTemplate
		{
			get
			{
				return this.headerTemplate;
			}
			set
			{
				this.headerTemplate = value;
			}
		}

		protected bool Initialized
		{
			get
			{
				return this._inited;
			}
		}

		protected bool IsBoundUsingDataSourceID
		{
			get
			{
				return (this.DataSourceID.Length > 0);
			}
		}

		[Browsable(false)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		[Description("Repeater_Items")]
		public virtual RepeaterItemCollection Items
		{
			get
			{
				if (this.itemsCollection == null)
				{
					if (this.itemsArray == null)
					{
						this.EnsureChildControls();
					}
					this.itemsCollection = new RepeaterItemCollection(this.itemsArray);
				}
				return this.itemsCollection;
			}
		}

		[PersistenceMode(PersistenceMode.InnerProperty)]
		[Browsable(false)]
		[TemplateContainer(typeof(RepeaterItem))]
		[Description("Repeater_ItemTemplate")]
		[DefaultValue((string) null)]
		public virtual ITemplate ItemTemplate
		{
			get
			{
				return this.itemTemplate;
			}
			set
			{
				this.itemTemplate = value;
			}
		}

		protected bool RequiresDataBinding
		{
			get
			{
				return this._requiresDataBinding;
			}
			set
			{
				this._requiresDataBinding = value;
			}
		}

		protected DataSourceSelectArguments SelectArguments
		{
			get
			{
				if (this._arguments == null)
				{
					this._arguments = this.CreateDataSourceSelectArguments();
				}
				return this._arguments;
			}
		}

		[Description("Repeater_SeparatorTemplate")]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[TemplateContainer(typeof(RepeaterItem))]
		[Browsable(false)]
		[DefaultValue((string) null)]
		public virtual ITemplate SeparatorTemplate
		{
			get
			{
				return this.separatorTemplate;
			}
			set
			{
				this.separatorTemplate = value;
			}
		}
	}
}
